package com.epam.web_project.bank.dao.factory;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.epam.web_project.bank.dao.account_dao.AccountDAO;
import com.epam.web_project.bank.dao.account_dao.MySQLAccountDAO;
import com.epam.web_project.bank.dao.customer_dao.CustomerDAO;
import com.epam.web_project.bank.dao.customer_dao.MySQLCustomerDAO;
import com.epam.web_project.bank.dao.transaction_dao.MySQLTransactionDAO;
import com.epam.web_project.bank.dao.transaction_dao.TransactionDAO;
import com.epam.web_project.bank.db.ConnectionPool;

public class MySQLDAOFactory extends DAOFactory {
	private static ConnectionPool connectionPool;
	private static Logger logger = Logger.getLogger(MySQLDAOFactory.class);

	public static Connection takeConnection() {
		try {
			return connectionPool.checkOut();
		} catch (InterruptedException e) {
			logger.error(e);
			return null;
		}
	}

	public static void returnConnection(Connection connection)
			throws SQLException {
		connectionPool.checkIn(connection);
	}

	@Override
	public void setConnectionPool(ConnectionPool pool) {
		connectionPool = pool;
	}

	@Override
	public CustomerDAO getCustomerDAO() {
		return new MySQLCustomerDAO();
	}

	@Override
	public AccountDAO getAccountDAO() {
		return new MySQLAccountDAO();
	}

	@Override
	public TransactionDAO getTransactionDAO() {
		return new MySQLTransactionDAO();
	}

}
